package com.example.androidapp2;

import com.xyzdesa.droidnomy.R;
import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class PopUpTarjeta  extends Dialog  {
	DbAdapter DbConect;
	Context Ctx;
	int idMov;
	private ImageView ibtnUpCuotas;
	private ImageView ibtnDwnCuotas;
	private ImageView ibtnUpPagos;
	private ImageView ibtnDwnPagos;
	private TextView txtCuotas;
	private TextView txtPagos;
	private Button btnAceptar;

	public PopUpTarjeta(Context context, DbAdapter db) {
		super(context);
		Ctx = context;
		DbConect = db;

		this.setContentView(R.layout.pup_tarjetas);
		this.setTitle(Ctx.getResources().getString(R.string.pupTarjeta));

		ibtnUpCuotas = (ImageView) this.findViewById(R.id.ibtnArriba);
		ibtnDwnCuotas = (ImageView) this.findViewById(R.id.ibtnAbajo);
		ibtnUpPagos = (ImageView) this.findViewById(R.id.ibtnArribaPago);
		ibtnDwnPagos = (ImageView) this.findViewById(R.id.ibtnAbajoPago);
		txtCuotas = (TextView) this.findViewById(R.id.txtCuotas);
		txtPagos = (TextView) this.findViewById(R.id.txtPagos);
		btnAceptar = (Button) this.findViewById(R.id.btnAceptar);

		SetListeners();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return true;
	}	

	public void show(int id)
	{
		idMov = id;

		txtCuotas.setText("1");
		txtPagos.setText("0");
		this.show();
	}

	public void SetListeners()
	{
		ibtnUpCuotas.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				int ct = Integer.parseInt(txtCuotas.getText().toString())+1;
				txtCuotas.setText(String.valueOf(ct));
			}
		});
		ibtnDwnCuotas.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				int ct = Integer.parseInt(txtCuotas.getText().toString())-1;
				if(ct >=1)
					txtCuotas.setText(String.valueOf(ct));
			}
		});
		ibtnUpPagos.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				int ct = Integer.parseInt(txtCuotas.getText().toString());				
				int pg = Integer.parseInt(txtPagos.getText().toString())+1;
				if(pg<=ct)
					txtPagos.setText(String.valueOf(pg));
			}
		});
		ibtnDwnPagos.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				int ct = Integer.parseInt(txtPagos.getText().toString())-1;
				if(ct >=0)
					txtPagos.setText(String.valueOf(ct));
			}
		});

		btnAceptar.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				DbConect.InsertTarjeta(idMov, Integer.parseInt(txtCuotas.getText().toString()), Integer.parseInt(txtPagos.getText().toString()),0);
				((MainActivity) Ctx).ActualizarProgressBar();
				((MainActivity) Ctx).CargarGrilla(DbConect.GetIngresos());
				hide();
			}
		});

	}

}
